package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;
import cn.com.winning.ecare.common.AsyncImageLoader;
import cn.com.winning.ecare.common.AsyncImageLoader.ImageDownloadCallBack;
import cn.com.winning.ecare.common.ClinicPathTextView;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.expandable.ActionSlideExpandableListView;
import cn.com.winning.ecare.utils.DisplayUtil;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.widgets.CustomProgressDialog;


/**医生列表
 * @author Administrator
 *
 */
public class Hospitalfour extends Activity
{
	private CustomProgressDialog proDialog;// 加载框
	private Hospitalfour oThis = this;
	private List<Map<String, Object>> list_ys = new ArrayList<Map<String, Object>>();
	private List<Map<String, Object>> list_yssx = new ArrayList<Map<String, Object>>();
	private String strKsId;
	private Intent mIntent;
	private MyAdapter myadapter;
	private ListView lv;
	private RelativeLayout layout;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hospital_four);
		MyApplication myApplication;
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		// 获取控件
		findView();

		// 获取数据
		getData();
	}

	private void findView()
	{
		list_yssx = new ArrayList<Map<String, Object>>();
		list_ys = new ArrayList<Map<String, Object>>();
		mIntent = getIntent();
		if (mIntent != null)
		{
			strKsId = mIntent.getStringExtra("strId");
		}
		lv = (ListView) this.findViewById(R.id.list_yssub);

	}

	private void getData()
	{
		proDialog = CustomProgressDialog.createDialog(this);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();
	}

	class LoadData implements Runnable
	{

		public void run()
		{
			// TODO Auto-generated method stub
			Looper.prepare();
			iniYs();
			Looper.loop();
		}
	}

	private void iniYs()
	{
		try
		{
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			ls.add(new BasicNameValuePair("id", strKsId));
			JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.HOSPITALNAME, ls);
			List<Map<String, Object>> tempmain = new ArrayList<Map<String, Object>>();
			List<Map<String, Object>> tempchildren = new ArrayList<Map<String, Object>>();
			Map<String, Object> tempmap = new HashMap<String, Object>();
			if (json != null && !json.equals(""))
			{
				if (!json.toString().equals("{\"msg\":\"系统错误！\",\"success\":\"false\"}"))
				{
					if (new JSONArray(json.getString("nodeinfo")).length()>0)
					{
						// 获取收费信息
						JSONArray jsonArr = new JSONArray(json.getString("nodeinfo"));
						JSONObject js = null;
						for (int i = 0; i < jsonArr.length(); i++)
						{
							Map<String, Object> map_ys = new HashMap<String, Object>();
							Map<String, Object> map_yssx = new HashMap<String, Object>();
							js = jsonArr.getJSONObject(i);
							map_ys.put("ys", js.getString("orgname"));
							//String ysId = js.getString("id");
							map_ys.put("ysId", js.getString("id"));
							map_ys.put("ysDj", js.getString("grade"));
							map_ys.put("ysRe", js.getString("remark"));
							map_yssx.put("yssx", js.getString("sxnr"));
							map_yssx.put("imgname", js.getString("imgpath"));

							// 获取图片
//							List<NameValuePair> ls1 = new ArrayList<NameValuePair>();
//							ls1.add(new BasicNameValuePair("id", ysId));
//							Bitmap bitmap = HTTPGetTool.getTool().queryByteForPost(URLUtils.HOST + URLUtils.HOSPITALPIC, ls1);
//							map_yssx.put("pic", bitmap);

							tempmain.add(map_ys);
							tempchildren.add(map_yssx);
						}
						msg.what = 0;
						tempmap.put("tempmain", tempmain);
						tempmap.put("tempchildren", tempchildren);
						msg.obj = tempmap;
					}
					else
					{
						msg.what = -1;
						msg.obj = "暂无数据";
					}

				}
				else
				{
					msg.what = -1;
					msg.obj = "系统错误";
				}

			}
			else
			{
				msg.what = -1;
				msg.obj = "网络连接失败!";
			}
			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			// 捕捉异常
			Message msg = myHandler.obtainMessage();
			msg.what = -1;
			msg.obj = e.toString();
			myHandler.sendMessage(msg);
		}
	}

	/**
	 * 主线程状态更新
	 */
	Handler myHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case -1:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					Toast.makeText(oThis, (String)msg.obj, Toast.LENGTH_SHORT).show();
					break;
				case 0:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					Map<String, Object> map = (Map<String, Object>) msg.obj;
					list_ys = (List<Map<String, Object>>) map.get("tempmain");
					list_yssx = (List<Map<String, Object>>) map.get("tempchildren");
					if (myadapter == null)
					{
						myadapter = new MyAdapter();
					}
					lv.setAdapter(myadapter);
					myadapter.notifyDataSetChanged();
					break;
				case 1:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					// setHosInfo();
					break;

				default:
					break;
			}
		};
	};

	private void setys()
	{
		myadapter = new MyAdapter();
		myadapter.notifyDataSetChanged();
		lv.setAdapter(myadapter);

	}

	/**
	 * 自定义listview布局显示内容
	 */
	class MyAdapter extends BaseAdapter
	{
		private AsyncImageLoader imageLoader = new AsyncImageLoader(getApplicationContext());
		public int getCount()
		{
			// TODO Auto-generated method stub
			return list_ys.size();
		}

		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return list_ys.get(position);
		}

		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			// TODO Auto-generated method stub
			View view = convertView;

			view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.hospital_four_sub, null);
			final String strys = (CharSequence) list_ys.get(position).get("ys") == null ? "" : list_ys.get(position)
					.get("ys").toString();
			final String strystc = (CharSequence) list_ys.get(position).get("ysRe") == null ? "" : list_ys.get(position)
					.get("ysRe").toString();
			final String strysdj = (CharSequence) list_ys.get(position).get("ysDj") == null ? "" : list_ys.get(position)
					.get("ysDj").toString();
			final String strYssx = (CharSequence) list_yssx.get(position).get("yssx") == null ? "" : list_yssx.get(position)
					.get("yssx").toString();
			final String ysId = (CharSequence) list_ys.get(position).get("ysId") == null ? "" : list_ys.get(position)
					.get("ysId").toString();
			final String imgname = (CharSequence) list_yssx.get(position).get("imgname") == null ? "" : list_yssx.get(position)
					.get("imgname").toString();
			
			TextView tv_ys = (TextView) view.findViewById(R.id.ys_name);
			tv_ys.setText(strys);
			TextView tv_yssx = (TextView) view.findViewById(R.id.tv_ys);
			tv_yssx.setText(strystc);
			Button tv_ysdj = (Button) view.findViewById(R.id.ys_dj);
			tv_ysdj.setText("  " + strysdj + "  ");

			ImageView iv = (ImageView) view.findViewById(R.id.ys_img);
			Bitmap bitmap = null;
			if(!imgname.equals("")){
				bitmap = imageLoader.loadImageByname(iv, imgname,URLUtils.HOST + URLUtils.HOSPITALPICMAIN,
						new ImageDownloadCallBack() {
							@Override
							public void onImageDownloaded(ImageView imageView,
									Bitmap bitmap) {
									imageView.setImageBitmap(bitmap);
								
							}
						});

				if (bitmap != null) {
					iv.setImageBitmap(bitmap);
				}
			}
			
			
			final Bitmap bp = bitmap;
			layout = (RelativeLayout) view.findViewById(R.id.layout_ghys);

			layout.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					// TODO Auto-generated method stub
					Intent intent = new Intent(Hospitalfour.this, HospitalFive.class);
					intent.putExtra("stryssx", strYssx);
					intent.putExtra("strys", strys);
					intent.putExtra("strysdj", strysdj);
					intent.putExtra("strystc", strystc);
					intent.putExtra("BITMAP", bp); // 这里可以放一个bitmap
					startActivity(intent);
				}
			});
			return view;
		}
	}

}
